<!DOCTYPE html>


<html lang="zh-CN">
  

    <head>
      <meta charset="utf-8" />
       
      <meta name="keywords" content="hexo,前端技术，前端，前端学习" />
       
      <meta name="description" content="努力小子的博客" />
      
      <meta
        name="viewport"
        content="width=device-width, initial-scale=1, maximum-scale=1"
      />
      <title>微前端 |  努力小子-博客</title>
  <meta name="generator" content="hexo-theme-ayer">
      
      <link rel="shortcut icon" href="/favicon.ico" />
       
<link rel="stylesheet" href="/dist/main.css">

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css"
      />
      
<link rel="stylesheet" href="/css/custom.css">
 
      <script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>
       
 

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/npm/@sweetalert2/theme-bulma@5.0.1/bulma.min.css"
      />
      <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.19/dist/sweetalert2.min.js"></script>

      <!-- mermaid -->
      
      <style>
        .swal2-styled.swal2-confirm {
          font-size: 1.6rem;
        }
      </style>
    </head>
  </html>
</html>


<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-weiqianduan"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  微前端
</h1>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2022/02/25/weiqianduan/" class="article-date">
  <time datetime="2022-02-24T16:00:00.000Z" itemprop="datePublished">2022-02-25</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E5%89%8D%E7%AB%AF%E5%BB%BA%E8%AE%BE/">前端建设</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">2.1k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">8 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h3 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h3><p>微前端是一门比较热门的技术架构。<br>微服务：一个应用分解成多个服务，每个服务独自服务内部的功能。例如原来的应用有abcd四个页面，现在分解成两个服务，第一个服务有ab两个页面，第二个服务有cd两个页面，组合在一起就和原来的应用一样。</p>
<p>当其中一个服务出现故障时，并不会影响其他服务的正常运行。</p>
<p>好处：不同的服务独立运行，服务与服务之间解耦。可以把各个服务理解成各个组件，类似前端的组件化。每个服务可以独自管理，修改一个服务不影响其他服务的运行。</p>
<p>难点：不容易确认服务的边界。当业务之间关联比较复杂时，很难拆分出各个独立的服务。</p>
<p>微前端：其实就是微服务在前端的应用。</p>
<h3 id="微前端实现方案"><a href="#微前端实现方案" class="headerlink" title="微前端实现方案"></a>微前端实现方案</h3><ul>
<li>单实例：单前页面只存在一个子应用，一般可以使用qiankun(阿里的微前端框架)</li>
<li>多实例：当前页面存在多个子应用，可以使用浏览器原生隔离方案，嵌套iframe。</li>
</ul>
<p>Tips：其实单实例也可以使用iframe，老一套的微前端技术几乎都是采用嵌iframe的方案。iframe最大的特性就是提供了浏览器原生的硬隔离方案，不论是样式隔离，js隔离这类问题统统都能被完美解决。但他的最大问题也在于他的隔离性无法被突破，导致应用间上下文无法被共享，随之带来的开发体验，产品体验问题。</p>
<h3 id="前置工作"><a href="#前置工作" class="headerlink" title="前置工作"></a>前置工作</h3><h5 id="搭建环境"><a href="#搭建环境" class="headerlink" title="搭建环境"></a>搭建环境</h5><p>主应用和子应用可以用vue和react创建，任意选择。每个应用就像开发单个项目那样用脚手架工具初始化项目就行。目录如下<br><img src="/images/dajianweiqianduan.png"></p>
<h5 id="应用注册"><a href="#应用注册" class="headerlink" title="应用注册"></a>应用注册</h5><p>在有了主应用之后，需要在主应用中注册各个子应用，参数包含如下：</p>
<ul>
<li>name: 子应用名词</li>
<li>entry: 子应用的资源入口</li>
<li>container: 主应用渲染子应用的节点</li>
<li>activeRule: 在哪些路由下渲染该子应用</li>
</ul>
<p>这些信息和我们在项目中注册路由很像，enry可以看做需要渲染的组件，container可以看做路由渲染的节点，activeRule可以看做如何匹配路由的规则。</p>
<p>注册子应用代码示例如下：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs plaintext">// src/types.ts<br>export interface IAppInfo &#123;<br>    name: string;<br>    entry: string;<br>    container: string;<br>    acticeRule: string;<br>&#125;<br><br>// src/start.ts<br>export const registerMicroApps = (appList: IAppInfo[]) =&gt; &#123;<br>    setAppList(appList)<br>&#125;<br><br>// src/appList/index.ts<br>let appList: IAppInfo[] = []<br><br>export const setAppList = (list: IAppInfo[]) =&gt; &#123;<br>    appList = list<br>&#125;<br><br>export const getAppList = () =&gt; &#123;<br>    return appList<br>&#125;<br></code></pre></td></tr></table></figure>
<p>上述实现很简单，就只需要将用户传入的 appList 保存起来即可</p>
<h5 id="路由劫持"><a href="#路由劫持" class="headerlink" title="路由劫持"></a>路由劫持</h5><p>在有了子应用列表以后，我们需要启动微前端以便渲染相应的子应用，也就是需要判断路由来渲染相应的应用。因此，我们可以先考虑如何监听路由的变化来判断渲染哪个子应用。</p>
<p>对于非SPA（单页应用）架构的项目来说，这个完全不是什么问题，因为我们只需要在启动微前端的时候判断下当前URL并渲染应用即可；但是在SPA架构下，路由变化是不会引发页面刷新的，因此我们需要一个方式知晓路由的变化，从而判断是否需要切换子应用或者什么事都不干。</p>
<p>可通过以下两种方式监听路由的变化：<br>1.hash模式，也就是URL中携带#<br>2.history模式，也就是常见的url格式</p>
<p>hash模式点击跳转会触发hashchange事件，history模式浏览器动作如点回退按钮会触发popstate事件。点击跳转会调用pushState函数向浏览器历史添加了一个状态，并且不会向服务器发起请求。</p>
<p>所以，主要涉及到两个事件：</p>
<ul>
<li>popstate</li>
<li>hashchange</li>
</ul>
<p>要去监听这两个事件，同时，调用pushState以及replaceState也会造成路由变化，但不会触发事件，因此要去重写这两个函数。</p>
<p>代码示例如下：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><code class="hljs $xslt">// src/route/index.ts<br><br>// 保存原有方法<br>const originalPuah = window.history.pushState;<br>const originalReplace = window.history.replaceState<br><br>export const hijackRoute = () =&gt; &#123;<br>    // 重写方法<br>    window.history.pushState = (...args) =&gt; &#123;<br>        // 调用原有方法<br>        originalPuah.apply(window.history, args)<br>        // URL 改变逻辑，实际就是如何处理子应用<br>        // ...<br>    &#125;;<br>    window.history.replaceState = (...args) =&gt; &#123;<br>        originalReplace.apply(window.history, args);<br>        // URL 改变逻辑，实际就是如何处理子应用<br>        // ...<br>    &#125;<br>    <br>    // 监听事件，触发URL改变逻辑<br>    window.addEventListener(&quot;hashchange&quot;, () =&gt; &#123;&#125;);<br>    window.addEventListener(&quot;popstate&quot;, () =&gt; &#123;&#125;);<br>    <br>   // 重写<br>    window.addEventListener = hijackEventListener(window.addEventListener);<br>    window.removeEventListener = hijackEventListener(window.removeEventListener);<br>&#125;<br><br>const capturedListeners: Record&lt;EventType, Function[]&gt; = &#123;<br>    hashchange: [],<br>    popstate: [],<br>&#125;<br><br>const hasListeners = (name: EventType, fn: Function) =&gt; &#123;<br>    return capturedListeners[name].filter((listener) =&gt; listener === fn).length;<br>&#125;<br>const hijackEventListener = (func: Function): any =&gt; &#123;<br>    return function (name: string, fn: Function)&#123;<br>        // 如果是以下事件，保存回调函数<br>        if(name === &#x27;hashchange&#x27; || name === &#x27;popstate&#x27;) &#123;<br>            if(!hasListeners(name, fn))&#123;<br>                capturedListeners[name].push(fn);<br>                return;<br>            &#125; else &#123;<br>                capturedListeners[name] = capturedListeners[name].filter(<br>                     (listener) =&gt; listener !== fn<br>                 );<br>            &#125;<br>        &#125;<br>        return func.apply(window, arguments);<br>    &#125;<br>&#125;<br><br>// 后续渲染子应用后使用，用于执行之前保存的回调函数<br>// 后续渲染子应用后使用，用于执行之前保存的回调函数<br>export function callCapturedListeners() &#123;<br>  if (historyEvent) &#123;<br>    Object.keys(capturedListeners).forEach((eventName) =&gt; &#123;<br>      const listeners = capturedListeners[eventName as EventType]<br>      if (listeners.length) &#123;<br>        listeners.forEach((listener) =&gt; &#123;<br>          // @ts-ignore<br>          listener.call(this, historyEvent)<br>        &#125;)<br>      &#125;<br>    &#125;)<br>    historyEvent = null<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>以上代码主要做了3件事：</p>
<ol>
<li>重写pushState 以及 replaceState 方法，在方法中调用原有方法后执行如何处理子应用的逻辑</li>
<li>监听 hashchange 及 popstate 事件，事件触发后执行如何处理子应用的逻辑</li>
<li>重写监听 / 移除事件函数，如果应用监听了 hashchange 及 popstate 事件就将回调函数保存起来以备后用</li>
</ol>
<h5 id="应用生命周期"><a href="#应用生命周期" class="headerlink" title="应用生命周期"></a>应用生命周期</h5><p>其实生命周期可以参考vue组件的生命周期，对于主应用来说，分为以下三个生命周期：</p>
<ol>
<li>beforeLoad: 挂载子应用前</li>
<li>mounted: 挂载子应用后</li>
<li>unmounted: 卸载子应用<br>还可以根据自己的需要添加生命周期。</li>
</ol>
<p>对于子应用来说，通常分为以下三个生命周期</p>
<ol>
<li>bootstrap：首次应用加载触发，常用于配置子应用全局信息</li>
<li>mount：应用挂载时触发，常用于渲染子应用</li>
<li>unmount：应用卸载时触发，常用于销毁子应用</li>
</ol>
<p>实现注册主应用生命周期函数， 代码示例：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs $xslt">// src/types.ts<br>export interface ILifeCycle &#123;<br>    beforeLoad?: LifeCycle | LifeCycle[];<br>    mounted?: LifeCycle | LifeCycle[];<br>    unmounted?: LifeCycle | LifeCycle[];<br>&#125;<br>// src/start.ts<br>// 改写下之前的<br>export const registerMicroApps = (<br>  appList: IAppInfo[],<br>  lifeCycle?: ILifeCycle<br>) =&gt; &#123;<br>  setAppList(appList);<br>  lifeCycle &amp;&amp; setLifeCycle(lifeCycle);<br>&#125;;<br><br>// src/lifeCycle/index.ts<br>let lifeCycle: ILifeCycle = &#123;&#125;;<br><br>export const setLifeCycle = (list: ILifeCycle) =&gt; &#123;<br>  lifeCycle = list;<br>&#125;;<br></code></pre></td></tr></table></figure>
<p>子应用的生命周期：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><code class="hljs $xslt">// src/enums.ts<br>// 设置子应用状态<br>export enum AppStatus &#123;<br>  NOT_LOADED = &quot;NOT_LOADED&quot;,<br>  LOADING = &quot;LOADING&quot;,<br>  LOADED = &quot;LOADED&quot;,<br>  BOOTSTRAPPING = &quot;BOOTSTRAPPING&quot;,<br>  NOT_MOUNTED = &quot;NOT_MOUNTED&quot;,<br>  MOUNTING = &quot;MOUNTING&quot;,<br>  MOUNTED = &quot;MOUNTED&quot;,<br>  UNMOUNTING = &quot;UNMOUNTING&quot;,<br>&#125;<br><br>// src/lifeCycle/index.ts<br>export const runBeforeLoad = async (app: IInternalAppInfo) =&gt; &#123;<br>  app.status = AppStatus.LOADING;<br>  await runLifeCycle(&quot;beforeLoad&quot;, app);<br><br>  app = await 加载子应用资源;<br>  app.status = AppStatus.LOADED;<br>&#125;;<br><br>export const runBoostrap = async (app: IInternalAppInfo) =&gt; &#123;<br>  if (app.status !== AppStatus.LOADED) &#123;<br>    return app;<br>  &#125;<br>  app.status = AppStatus.BOOTSTRAPPING;<br>  await app.bootstrap?.(app);<br>  app.status = AppStatus.NOT_MOUNTED;<br>&#125;;<br><br>export const runMounted = async (app: IInternalAppInfo) =&gt; &#123;<br>  app.status = AppStatus.MOUNTING;<br>  await app.mount?.(app);<br>  app.status = AppStatus.MOUNTED;<br>  await runLifeCycle(&quot;mounted&quot;, app);<br>&#125;;<br><br>export const runUnmounted = async (app: IInternalAppInfo) =&gt; &#123;<br>  app.status = AppStatus.UNMOUNTING;<br>  await app.unmount?.(app);<br>  app.status = AppStatus.NOT_MOUNTED;<br>  await runLifeCycle(&quot;unmounted&quot;, app);<br>&#125;;<br><br>const runLifeCycle = async (name: keyof ILifeCycle, app: IAppInfo) =&gt; &#123;<br>  const fn = lifeCycle[name];<br>  if (fn instanceof Array) &#123;<br>    await Promise.all(fn.map((item) =&gt; item(app)));<br>  &#125; else &#123;<br>    await fn?.(app);<br>  &#125;<br>&#125;;<br></code></pre></td></tr></table></figure>
<p>以上代码主要做了以下几件事</p>
<ul>
<li>设置子应用状态，用于逻辑判断以及优化。比如说当一个应用状态为非 NOT_LOADED 时（每个应用初始都为 NOT_LOADED 状态），下次渲染该应用时就无需重复加载资源了</li>
<li>如需要处理逻辑，比如说 beforeLoad 我们需要加载子应用资源</li>
<li>执行主 / 子应用生命周期，这里需要注意下执行顺序，可以参考父子组件的生命周期执行顺序</li>
</ul>
<p>…未完待续…</p>
 
      <!-- reward -->
      
      <div id="reword-out">
        <div id="reward-btn">
          打赏
        </div>
      </div>
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=https://wenshijun.gitee.io/2022/02/25/weiqianduan/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E5%89%8D%E7%AB%AF%E5%BB%BA%E8%AE%BE/" rel="tag">前端建设</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E5%BE%AE%E5%89%8D%E7%AB%AF/" rel="tag">微前端</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2022/09/26/ssr_spa/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            ssr和spa
          
        </div>
      </a>
    
    
      <a href="/2022/02/25/%E5%89%8D%E7%AB%AF%E6%B2%99%E7%AE%B1/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">前端沙箱</div>
      </a>
    
  </nav>

  
   
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2022
        <i class="ri-heart-fill heart_icon"></i> wenshijun
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>访问人数:<span id="busuanzi_value_site_uv"></span></span>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>浏览次数:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>    
    </main>
    <div class="float_btns">
      <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

    </div>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/logo.png" alt="努力小子-博客"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about/me">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="搜索">
        <i class="ri-search-line"></i>
      </a>
      
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝碗白粥吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/weixin.png">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-3.6.0.min.js"></script>
 
<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->
 
<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: ".tocbot",
    contentSelector: ".article-entry",
    headingSelector: "h1, h2, h3, h4, h5, h6",
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: "main",
    positionFixedSelector: ".tocbot",
    positionFixedClass: "is-position-fixed",
    fixedSidebarOffset: "auto",
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link
  rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css"
/>
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->
 <!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script> 
<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->
 
<script src="/js/busuanzi-2.3.pure.min.js"></script>
 
<!-- ClickLove -->
 
<script src="/js/clickLove.js"></script>
 
<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->
 
<link rel="stylesheet" href="/css/clipboard.css">
 <script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>
 
<!-- CanvasBackground -->
 
<script src="/js/dz.js"></script>
 
<script>
  if (window.mermaid) {
    mermaid.initialize({ theme: "forest" });
  }
</script>


    
    

  </div>
</body>

</html>